package com.zhucan.account.application.query.qry;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * author: zhuCan
 * date: 2020/8/5 10:26
 * description:
 */
@Data
public abstract class AbstractPage {

  protected Integer page;

  protected Integer perPage;

  protected String field;

  protected String order;

  public Pageable pageable() {
    if (page == null || page < 0) {
      page = 0;
    }

    if (perPage == null || perPage < 0) {
      perPage = 10;
    }

    if (StringUtils.isNotBlank(field) && StringUtils.isNotBlank(order)) {
      return PageRequest.of(page, perPage, Sort.by("descend".equals(order) ? Sort.Direction.DESC : Sort.Direction.ASC, field));
    }

    return PageRequest.of(page, perPage);
  }

}
